//
//  SuggestionsBoxViewController.m
//  QuickCure
//
//  Created by 马太行 on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "SuggestionsBoxViewController.h"
#import "SuggestionsBoxTableViewCell.h"
#import <SVProgressHUD.h>
#import "SettingViewController.h"
@interface SuggestionsBoxViewController ()<UITableViewDelegate,UITableViewDataSource,UITextViewDelegate>

//上方的View
@property (nonatomic,weak) UIView *upView;

//输入框
@property (nonatomic,weak) UITextView *textView;

//剩余字数
@property (nonatomic,weak) UILabel *labelnum;

@property (nonatomic,weak) UILabel *removelabel;
@end

@implementation SuggestionsBoxViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景色
    self.view.backgroundColor= [UIColor whiteColor];
    
    //使用tablevView
    UITableView *SuggestionBoxTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 300, 375, 300)];
    
    //行高
    SuggestionBoxTableView.rowHeight = 45;
    
    //去除多余的空行   !!
    SuggestionBoxTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    //添加
    [self.view addSubview:SuggestionBoxTableView];
    
    //设置代理
    SuggestionBoxTableView.delegate = self;
    SuggestionBoxTableView.dataSource = self;
    _textView.delegate = self;

    //调用添加的控件
    [self upViewPart];

    //辩题
    self.navigationItem.title = @"意见反馈";
    
    //左上角白箭头
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"jiantou"] style:UIBarButtonItemStyleDone target:self action:@selector(popview)];
    //主题色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

}
-(void)popview
{
    //返回上一层
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    //返回组
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //返回行
    
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    //加载cell
    SuggestionsBoxTableViewCell *cell = [[SuggestionsBoxTableViewCell alloc]initWithsuggestinonsBox:indexPath];
    
    
    return cell;
}
- (void)upViewPart
{
    //一个头view上面放了一个textfiled   一个label   一个button   还有两个label
    UIView *upView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 300)];
    //属性
    _upView = upView;
    
    //textField
    [self textField];
    
    //submitButton
    [self submitButton];
    
    //elseLabel
    [self elseLabel];
    
    //添加view
    [self.view addSubview:upView];


}

//textField  上发输入框
- (void)textField
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
    
    label.text = @"请输入反馈信息";
    _removelabel =label;
    
    label.textColor = [UIColor grayColor];
    //大小
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(5, 5, 365, 200)];
    
    [textView setDelegate:self];
    
    //自动联想关闭
    [textView setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    
    
    //属性
    _textView = textView;
    
    //键盘样式
    textView.keyboardType = UIKeyboardTypeDefault;
    
    //背景色  淡灰色
    textView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //添加field
    [_upView addSubview:textView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textclick:) name:@"UITextViewTextDidChangeNotification" object:nil];
    
    
    [_textView addSubview:label];
}
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    
//    if ([text isEqualToString:@""]) {
//        
//        
//        return YES;
//    }
//    
//    if (textView.text.length==200) {
//        
//        
//        return NO;
//    }
//
//
//
//
//    return YES;
//
//}



- (void)textclick:(UITextView *)textfield
{
   
    if (_textView.text.length) {
         _removelabel.text = @"";
    }
    else
    {
     _removelabel.text = @"请输入反馈信息";
    }
    
    
    
   // NSLog(@"%lu",_textfield.text.length);
    _labelnum.text = [NSString stringWithFormat:@"您还可以输入的字数为:%lu",200 - _textView.text.length];
    if (_textView.text.length >= 200 ) {
      
        _labelnum.textColor = [UIColor redColor];
        
        _labelnum.text = @"您还可以输入的字数为:0";
    } if (_textView.text.length < 200) {
        
        _labelnum.textColor = [UIColor blackColor];

        }
  
}

//提交的btn
- (void)submitButton
{
    //提交按钮
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(260, 210, 100, 40)];
    
    //按钮点击事件
    [btn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
    //背景色
    [btn setBackgroundImage:[UIImage imageNamed:@"dijiaofankui"] forState:UIControlStateNormal];
    
    //添加btn
    [_upView addSubview:btn];
}

//btn的点击事件
- (void)submit
{
    if (_textView.text.length ==0) {
        
        // 遮布显示时间
        [SVProgressHUD setMinimumDismissTimeInterval:0.1];
        
        //遮布
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        //感叹号+字
        [SVProgressHUD showInfoWithStatus:@"请输入反馈信息"];
    }
    if (_textView.text.length < 200 &&_textView.text.length > 0 )
    {
        //pop回去
        [self.navigationController popViewControllerAnimated:YES];

        // 遮布显示时间
        [SVProgressHUD setMinimumDismissTimeInterval:0.5];
        
        //遮布
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        //感叹号+字
        [SVProgressHUD showSuccessWithStatus:@"谢谢您的意见反馈"];
    }
    
    
    if (_textView.text.length >=200) {
        // 遮布显示时间
        [SVProgressHUD setMinimumDismissTimeInterval:0.5];
        
        //遮布
        [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
        
        //感叹号+字
        [SVProgressHUD showErrorWithStatus:@"字体超出"];
    }
}

- (void)elseLabel
{
    //提示的label
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 210, 230, 40)];
    
    _labelnum = label;
 
    //初始化文本
    label.text =[NSString stringWithFormat:@"您还可以输入的字数为:%lu",200 - _textView.text.length];
    
    //添加label
    [_upView addSubview:label];
    
    
    //欢迎label
    UILabel *welcomeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 255, 375, 45)];
    
    //背景色
    welcomeLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //文本信息
    welcomeLabel.text = @"    欢迎联系我们";
    
    //添加
    [_upView addSubview:welcomeLabel];
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        //alertcontroller
        UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否拨打电话: 400-636-6113" preferredStyle:UIAlertControllerStyleAlert];
        
        //设置两个
        UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
       
        //先添加的在前面
        [alertController addAction:cancel];
        [alertController addAction:confirm];
        
        //加到nv
        [self.navigationController presentViewController:alertController animated:YES completion:nil];
    
    }
    if (indexPath.row == 1) {
        
    }
}

//点击空白处收回键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [_textView resignFirstResponder];
}

@end
